package com.edu.orderservice.controller;


import com.edu.commonutils.R;
import com.edu.orderservice.service.TPayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author ljw
 * @since 2022-09-26
 */
@Api("订单流水详情")
//@CrossOrigin
@RestController
@RequestMapping("/orderservice/paylog")
public class TPayLogController {

    @Autowired
    private TPayLogService payService;
    /**
     * 生成二维码
     *
     * @return
     */
    //微信支付接口
    @ApiOperation("生成二维码")
    @GetMapping("/createNative/{orderNo}")
    @ResponseBody
    public R createNative(@PathVariable String orderNo) {
        Map map = payService.createNative(orderNo);
        return R.ok().data(map);
    }

    //根据订单号获取支付状态
    @PostMapping("/get/{orderNo}")
    @ResponseBody
    public R getByid(@PathVariable("orderNo") String orderNo) {
        //调用查询接口
        Map<String, String> map = payService.queryPayStatus(orderNo);
        if (map == null) {//出错
            return R.error().message("支付出错");
        }
        if (map.get("trade_state").equals("SUCCESS")) {//如果成功
            //更改订单状态
            payService.updateOrderStatus(map);

            return R.ok().message("支付成功");
        }

        return R.ok().code(25000).message("支付中");
    }


}

